package com.guptarulie.jmeter.protocol.mm7.deliveryrequest.control;

import org.apache.jmeter.testelement.AbstractTestElement;
import org.apache.jmeter.testelement.property.BooleanProperty;
import org.apache.jmeter.testelement.property.IntegerProperty;
import org.apache.jorphan.logging.LoggingManager;
import org.apache.log.Logger;

//For unit tests, @see TestMM7MirrorControl

/**
 * Test element that implements the Workbench MM7 Delivery Request Receiver function
 */
public class MM7DeliveryRequestReceiverControl extends AbstractTestElement{

    private static final long serialVersionUID = 233L;
    private static final Logger log = LoggingManager.getLoggerForClass();
    private transient MM7DeliveryRequestReceiverServer server;

    private static final int DEFAULT_PORT = 8081;
    public static final String DEFAULT_VASP_ID = "My ID";

    // and as a string
    public static final String DEFAULT_PORT_S =
        Integer.toString(DEFAULT_PORT);// Used by GUI

    public static final String PORT = "MM7DeliveryRequestReceiverControlGui.port"; // $NON-NLS-1$
    public static final String VASP_ID = "MM7DeliveryRequestReceiverControlGui.vasp_id"; // $NON-NLS-1$
    public static final String SRVC_CODE = "MM7DeliveryRequestReceiverControlGui.srvc_code"; // $NON-NLS-1$
    public static final String RCPT_ADDR = "MM7DeliveryRequestReceiverControlGui.rcpt_addr"; // $NON-NLS-1$
    public static final String PRIORITY = "MM7DeliveryRequestReceiverControlGui.priority"; // $NON-NLS-1$
    public static final String MSG_CLASS = "MM7DeliveryRequestReceiverControlGui.msg_class"; // $NON-NLS-1$
    public static final String SUBJECT = "MM7DeliveryRequestReceiverControlGui.subject"; // $NON-NLS-1$
    public static final String CHARGED_PARTY = "MM7DeliveryRequestReceiverControlGui.charged_party"; // $NON-NLS-1$
    public static final String DELIVERY_REPORT = "MM7DeliveryRequestReceiverControlGui.delivery_report"; // $NON-NLS-1$
    public static final String READ_REPLY_REPORT = "MM7DeliveryRequestReceiverControlGui.read_reply_report"; // $NON-NLS-1$
    
    public MM7DeliveryRequestReceiverControl(Object obj) {
        initPort(DEFAULT_PORT);  
    }

    public MM7DeliveryRequestReceiverControl(int port, Object obj) {
        initPort(port);
    }

    private void initPort(int port){
        setProperty(new IntegerProperty(PORT, port));
    }

    public void setPort(int port) {
        initPort(port);
    }

    public void setPort(String port) {
        setProperty(PORT, port);
    }

    public int getPort() {
        return getPropertyAsInt(PORT);
    }

    public String getPortString() {
        return getPropertyAsString(PORT);
    }

    public int getDefaultPort() {
        return DEFAULT_PORT;
    }

    public void setVaspID(String vaspID) {
        setProperty(VASP_ID, vaspID);
    }

    public String getVaspID() {
        return getPropertyAsString(VASP_ID);
    }

    public void setChargedParty(String chargedParty) {
        setProperty(CHARGED_PARTY, chargedParty);
    }

    public String getChargedParty() {
        return getPropertyAsString(CHARGED_PARTY);
    }
    
    public void setRecipientAddress(String rcptAddr) {
        setProperty(RCPT_ADDR, rcptAddr);
    }

    public String getRecipientAddress() {
        return getPropertyAsString(RCPT_ADDR);
    }

    public void setServiceCode(String srvcCode) {
        setProperty(SRVC_CODE, srvcCode);
    }

    public String getServiceCode() {
        return getPropertyAsString(SRVC_CODE);
    }

    public void setPriority(String priority) {
        setProperty(PRIORITY, priority);
    }

    public String getPriority() {
        return getPropertyAsString(PRIORITY);
    }

    public void setSubject(String subject) {
        setProperty(SUBJECT, subject);
    }

    public String getSubject() {
        return getPropertyAsString(SUBJECT);
    }

    public void setMsgClass(int msgClass) {
        setProperty(new IntegerProperty(MSG_CLASS, msgClass));
    }

    public int getMsgClass() {
        return getPropertyAsInt(MSG_CLASS);
    }

    public void setDeliveryReport(Boolean dlvryReport) {
        setProperty(new BooleanProperty(DELIVERY_REPORT, dlvryReport));
    }

    public Boolean getDeliveryReport() {
        return getPropertyAsBoolean(DELIVERY_REPORT);
    }

    public void setReadReplyReport(Boolean readReplyReport) {
        setProperty(new BooleanProperty(READ_REPLY_REPORT, readReplyReport));
    }

    public Boolean getReadReplyReport() {
        return getPropertyAsBoolean(READ_REPLY_REPORT);
    }

    
    @SuppressWarnings("unchecked")
	public Class getGuiClass() {
        return com.guptarulie.jmeter.protocol.mm7.deliveryrequest.control.gui.MM7DeliveryRequestReceiverControlGui.class;
    }
    


    public void startDeliveryReceiver(Object observerObj) {
    	MM7ConfigurableParameters mm7Parameters = new MM7ConfigurableParameters();
    	mm7Parameters.vaspID = getVaspID();
    	mm7Parameters.serviceCode = getServiceCode();
    	switch(getMsgClass())
    	{
    	case 0:
    		mm7Parameters.msgClass = "Advertisement";
    		break;
    		
    	case 1:
    		mm7Parameters.msgClass = "Information Service";
    		break;
    		
    	case 2:
    		mm7Parameters.msgClass = "Accounting";
    		break;
    	default:
    		log.error("Message class selected is :" + getMsgClass() + "Some problem...");
    		return;
    	}
 
    	mm7Parameters.chargedParty = getChargedParty();
    	mm7Parameters.priority = getPriority();
    	
    	if (getDeliveryReport() == true) 
    		mm7Parameters.deliveryReport = "TRUE";
    	else
    		mm7Parameters.deliveryReport = "FALSE";
     	
    	if (getReadReplyReport() == true) 
    		mm7Parameters.readReplyReport = "TRUE";
    	else
    		mm7Parameters.readReplyReport = "FALSE";
    
    	mm7Parameters.recipientAddr = getRecipientAddress();
    	mm7Parameters.subject = getSubject();
    	
    	// Create an instance of server and start...
        server = new MM7DeliveryRequestReceiverServer(getPort(), mm7Parameters, observerObj);
        server.start();
    }

    public void stopMM7DeliveryReceiver() {
        if (server != null) {
            server.stopServer();
            try {
                server.join(1000); // wait for server to stop
            } catch (InterruptedException e) {
            }
            server = null;
        }
    }

    public boolean canRemove() {
        return null == server;
    }
}
